﻿' ******************************************************************************
' ** 
' **  Yahoo! Managed
' **  Written by Marius Häusler 2011
' **  It would be pleasant, if you contact me when you are using this code.
' **  Contact: YahooFinanceManaged@gmail.com
' **  Project Home: http://code.google.com/p/yahoo-finance-managed/
' **  
' ******************************************************************************
' **  
' **  Copyright 2011 Marius Häusler
' **  
' **  Licensed under the Apache License, Version 2.0 (the "License");
' **  you may not use this file except in compliance with the License.
' **  You may obtain a copy of the License at
' **  
' **    http://www.apache.org/licenses/LICENSE-2.0
' **  
' **  Unless required by applicable law or agreed to in writing, software
' **  distributed under the License is distributed on an "AS IS" BASIS,
' **  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
' **  See the License for the specific language governing permissions and
' **  limitations under the License.
' ** 
' ******************************************************************************


Namespace YahooManaged.Search.BOSS.API



    Public Class SearchClientOptions
        Public Property ConsumerKey As String = String.Empty
        Public Property ConsumerSecret As System.Security.SecureString
        Public Property HttpsUsed() As Boolean = False

        Public Sub New()
        End Sub
        Friend Sub New(ByVal original As SearchClientOptions)
            With original
                Me.ConsumerKey = .ConsumerKey
                Me.ConsumerSecret = .ConsumerSecret.Copy
                Me.HttpsUsed = .HttpsUsed
            End With
        End Sub
    End Class


    Public MustInherit Class SearchOptions
        Inherits ResultCountOptions
        Implements IQueryOptions

        Friend MustOverride ReadOnly Property ServiceName As String


        Public Property QueryText As String = String.Empty Implements IQueryOptions.QueryText

        Public Property Culture() As Culture = Culture.DefaultCultures.UnitedStates_English
        Public Property RestrictedSites() As List(Of Uri) = New List(Of Uri)

        Public Sub New()
        End Sub
        Friend Sub New(ByVal original As SearchOptions)
            Me.RestrictedSites = New List(Of Uri)
            If original IsNot Nothing Then
                With original
                    Me.QueryText = .QueryText
                    If .Culture IsNot Nothing Then : Me.Culture = .Culture.CloneStrict
                    Else : Me.Culture = Nothing
                    End If
                    If .RestrictedSites IsNot Nothing Then
                        For Each siteRest As Uri In .RestrictedSites
                            Me.RestrictedSites.Add(siteRest)
                        Next
                    End If
                End With
            End If
        End Sub

    End Class


    Public Class WebSearchOptions
        Inherits SearchOptions
        Private mAllowAdultContent As Boolean = True
        Private mAllowedFileTypes(-1) As WebFileType
        Private mAllowedFileGroups(-1) As FileTypeGroup
        Private mLongAbstract As Boolean = False
        Private mLimitedWeb As Boolean = False
        Private mHtmlTaggedText As Boolean = True
        Private mRestrictedTitle As String = String.Empty
        Private mRestrictedUrl As String = String.Empty
        Private mSearchMonkey As Boolean = False
        Private mLanguageInfo As Boolean = False

        Friend Overrides ReadOnly Property ServiceName As String
            Get
                If Me.LimitedWeb Then
                    Return "limitedweb"
                Else
                    Return "web"
                End If
            End Get
        End Property

        ''' <summary>
        ''' Returns minimum 3 days old search results. Costs the half fee of full web search.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property LimitedWeb As Boolean
            Get
                Return mLimitedWeb
            End Get
            Set(ByVal value As Boolean)
                mLimitedWeb = value
            End Set
        End Property
        ''' <summary>
        ''' Filters out hate and porn content.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property AllowAdultContent() As Boolean
            Get
                Return mAllowAdultContent
            End Get
            Set(ByVal value As Boolean)
                mAllowAdultContent = value
            End Set
        End Property
        ''' <summary>
        ''' Restrict search results to setted file types.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property AllowedFileTypes() As WebFileType()
            Get
                Return mAllowedFileTypes
            End Get
            Set(ByVal value As WebFileType())
                If value IsNot Nothing Then
                    mAllowedFileTypes = value
                Else
                    mAllowedFileTypes = New WebFileType() {}
                End If
            End Set
        End Property
        ''' <summary>
        ''' Will retrieve and display an abstract of a web document up to 300 characters.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property LongAbstract() As Boolean
            Get
                Return mLongAbstract
            End Get
            Set(ByVal value As Boolean)
                mLongAbstract = value
            End Set
        End Property
        ''' <summary>
        ''' Will return search results with the title containing the value.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property RestrictedTitle As String
            Get
                Return mRestrictedTitle
            End Get
            Set(ByVal value As String)
                mRestrictedTitle = value
            End Set
        End Property
        ''' <summary>
        ''' Will return search results with the URL containing the value.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property RestrictedUrl As String
            Get
                Return mRestrictedUrl
            End Get
            Set(ByVal value As String)
                mRestrictedUrl = value
            End Set
        End Property
        ''' <summary>
        ''' Same effect like AllowedFileTypes property.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property AllowedFileGroups() As FileTypeGroup()
            Get
                Return mAllowedFileGroups
            End Get
            Set(ByVal value As FileTypeGroup())
                If value IsNot Nothing Then
                    mAllowedFileGroups = value
                Else
                    mAllowedFileGroups = New FileTypeGroup() {}
                End If
            End Set
        End Property
        ''' <summary>
        ''' If [FALSE], cleans out the HTML tags from the abstract.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property HtmlTaggedText() As Boolean
            Get
                Return mHtmlTaggedText
            End Get
            Set(ByVal value As Boolean)
                mHtmlTaggedText = value
            End Set
        End Property
        ''' <summary>
        ''' Provides additional data in Microformat.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property SearchMonkey As Boolean
            Get
                Return mSearchMonkey
            End Get
            Set(ByVal value As Boolean)
                mSearchMonkey = value
            End Set
        End Property
        ''' <summary>
        ''' Identifies the language of the result/document.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property LanguageInfo As Boolean
            Get
                Return mLanguageInfo
            End Get
            Set(ByVal value As Boolean)
                mLanguageInfo = value
            End Set
        End Property

        Public Sub New()
            Me.Count = 50
        End Sub
        Friend Sub New(ByVal original As WebSearchOptions)
            MyBase.New(original)
            If original IsNot Nothing Then
                With original
                    mLongAbstract = .LongAbstract
                    mAllowAdultContent = .AllowAdultContent
                    Dim ftlst As New List(Of WebFileType)
                    For Each ft In .AllowedFileTypes
                        ftlst.Add(ft)
                    Next
                    mAllowedFileTypes = ftlst.ToArray
                    Dim fglst As New List(Of FileTypeGroup)
                    For Each fg In .AllowedFileGroups
                        fglst.Add(fg)
                    Next
                    mAllowedFileGroups = fglst.ToArray
                    mLimitedWeb = .LimitedWeb
                    mHtmlTaggedText = .HtmlTaggedText
                    mRestrictedTitle = .RestrictedTitle
                    mRestrictedUrl = .RestrictedUrl
                End With
            End If
        End Sub
    End Class




    Public Class NewsSearchOptions
        Inherits SearchOptions

        Private mAlwaysLatestDateNow As Boolean = True
        Private mLatestDate As Date = Date.Now
        Private mTimeSpan As New TimeSpan(30, 0, 0, 0)
        Private mSorting As NewsSortRanking = NewsSortRanking.None
        Private mRestrictedTitle As String = String.Empty
        Private mRestrictedUrl As String = String.Empty

        Friend Overrides ReadOnly Property ServiceName As String
            Get
                Return "news"
            End Get
        End Property

        ''' <summary>
        ''' Indicates if the [LatestDate] Property will return actual DateTime.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property AlwaysLatestDateNow As Boolean
            Get
                Return mAlwaysLatestDateNow
            End Get
            Set(ByVal value As Boolean)
                mAlwaysLatestDateNow = value
            End Set
        End Property
        ''' <summary>
        ''' The maximum date of crawling date/time. Setting a date will deactivate [AlwaysLatestDateNow].
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property LatestDate() As Date
            Get
                If mAlwaysLatestDateNow Then
                    Return Date.Now
                Else
                    Return mLatestDate
                End If
            End Get
            Set(ByVal value As Date)
                mLatestDate = value
                mAlwaysLatestDateNow = False
            End Set
        End Property
        ''' <summary>
        ''' The timespan from latest date to minimum date of crawling date/time.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property TimeSpan() As TimeSpan
            Get
                Return mTimeSpan
            End Get
            Set(ByVal value As TimeSpan)
                If value.TotalSeconds > 0 Then
                    Dim absValue As TimeSpan = value
                    If value.TotalSeconds < 0 Then absValue = value.Negate
                    If absValue.TotalDays / 7 < 1000 Then : mTimeSpan = absValue
                    Else : mTimeSpan = New TimeSpan(999 * 7, 0, 0, 0)
                    End If
                Else : mTimeSpan = New TimeSpan(30, 0, 0, 0)
                End If
            End Set
        End Property
        ''' <summary>
        ''' The calculated minimum date of crawling date/time.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public ReadOnly Property EarliestDate() As Date
            Get
                Return mLatestDate.Subtract(mTimeSpan)
            End Get
        End Property
        ''' <summary>
        ''' The kind of sorting of results. Influences priority of result index.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Sorting() As NewsSortRanking
            Get
                Return mSorting
            End Get
            Set(ByVal value As NewsSortRanking)
                mSorting = value
            End Set
        End Property
        ''' <summary>
        ''' Will return search results with the title containing the value.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property RestrictedTitle As String
            Get
                Return mRestrictedTitle
            End Get
            Set(ByVal value As String)
                mRestrictedTitle = value
            End Set
        End Property
        ''' <summary>
        ''' Will return search results with the URL containing the value.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property RestrictedUrl As String
            Get
                Return mRestrictedUrl
            End Get
            Set(ByVal value As String)
                mRestrictedUrl = value
            End Set
        End Property

        Public Sub New()
            Me.Count = 50
        End Sub
        Friend Sub New(ByVal original As NewsSearchOptions)
            MyBase.New(original)
            If original IsNot Nothing Then
                With original
                    mLatestDate = .LatestDate
                    mTimeSpan = .TimeSpan
                    mSorting = .Sorting
                    mRestrictedTitle = .RestrictedTitle
                    mRestrictedUrl = .RestrictedUrl
                End With
            End If
        End Sub

    End Class






    Public Class ImageSearchOptions
        Inherits SearchOptions

        Private mAllowAdultContent As Boolean = False
        Private mDimensions As ImageSearchDimensions = ImageSearchDimensions.All
        Private mUrl As Uri = Nothing
        Private mRefererUrl As Uri = Nothing

        Friend Overrides ReadOnly Property ServiceName As String
            Get
                Return "images"
            End Get
        End Property

        ''' <summary>
        ''' Activate/Deactivates the Offensive Content Reduction filter.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property AllowAdultContent() As Boolean
            Get
                Return mAllowAdultContent
            End Get
            Set(ByVal value As Boolean)
                mAllowAdultContent = value
            End Set
        End Property
        ''' <summary>
        ''' The image size.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Dimensions As ImageSearchDimensions
            Get
                Return mDimensions
            End Get
            Set(ByVal value As ImageSearchDimensions)
                mDimensions = value
            End Set
        End Property
        ''' <summary>
        ''' Search for this URL. Returns this exact image result.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property Url As Uri
            Get
                Return mUrl
            End Get
            Set(ByVal value As Uri)
                mUrl = value
            End Set
        End Property
        ''' <summary>
        ''' Search for this URL. Depending on other query restrictions, returns all image objects with this referring URL.
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property RefererUrl As Uri
            Get
                Return mRefererUrl
            End Get
            Set(ByVal value As Uri)
                mRefererUrl = value
            End Set
        End Property

        Public Sub New()
            Me.Count = 35
        End Sub
        Friend Sub New(ByVal original As ImageSearchOptions)
            MyBase.New(original)
            If original IsNot Nothing Then
                With original
                    mAllowAdultContent = .AllowAdultContent
                    mDimensions = .Dimensions
                    mUrl = .Url
                    mRefererUrl = .RefererUrl
                End With
            End If
        End Sub


    End Class



    Public Class SpellingSearchOptions
        Inherits SearchOptions


        Friend Overrides ReadOnly Property ServiceName As String
            Get
                Return "spelling"
            End Get
        End Property

        Public Sub New()
            Me.Count = 1
        End Sub
        Friend Sub New(ByVal original As SpellingSearchOptions)
            MyBase.New(original)
        End Sub

    End Class

End Namespace